package com.share.controller;

import com.share.common.core.web.controller.BaseController;
import com.share.common.core.web.domain.AjaxResult;
import com.share.common.core.web.page.TableDataInfo;
import com.share.common.log.annotation.Log;
import com.share.common.log.enums.BusinessType;
import com.share.common.security.annotation.RequiresPermissions;
import com.share.domain.MySql.Cabinet;
import com.share.service.ICabinetService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Tag(name = "柜机管理业务接口")
@RestController
@RequestMapping("/cabinet")
public class CabinetController extends BaseController {

    @Autowired
    private ICabinetService cabinetService;


    /**
     * 搜索未使用柜机
     *
     * @param keyword
     * @return
     */
    @Operation(summary = "搜索未使用柜机")
    @GetMapping("/searchNoUseList/{keyword}")
    public AjaxResult searchNoUseList(@PathVariable String keyword) {
        List<Cabinet> noUseList = cabinetService.searchNoUseList(keyword);
        return success(noUseList);
    }


    /**
     * 删除柜机接口
     *
     * @param ids
     * @return
     */
    @Operation(summary = "删除柜机接口")
    @Log(title = "柜机管理", businessType = BusinessType.DELETE)
    @RequiresPermissions("device:cabinet:remove")
    @DeleteMapping("{ids}")
    public AjaxResult deleteByIds(@PathVariable Long[] ids) {
        boolean removeBatchByIds = cabinetService.removeBatchByIds(Arrays.asList(ids));
        AjaxResult ajaxResult = toAjax(removeBatchByIds);
        return ajaxResult;
    }


    /**
     * 修改柜机接口
     *
     * @param cabinet
     * @return
     */
    @Operation(summary = "修改柜机接口")
    @Log(title = "柜机管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("device:cabinet:edit")
    @PutMapping
    public AjaxResult update(@RequestBody Cabinet cabinet) {
        boolean flag = cabinetService.updateById(cabinet);
        AjaxResult ajaxResult = toAjax(flag);
        return ajaxResult;
    }


    /**
     * 添加柜机接口
     *
     * @param cabinet
     * @return
     */
    @Operation(summary = "添加柜机接口")
    @Log(title = "柜机管理", businessType = BusinessType.INSERT)
    @RequiresPermissions("device:cabinet:add")
    @PostMapping
    public AjaxResult add(@RequestBody Cabinet cabinet) {
        boolean flag = cabinetService.save(cabinet);
        AjaxResult ajaxResult = toAjax(flag);
        return ajaxResult;
    }


    /**
     * 根据id查询接口
     *
     * @param id
     * @return
     */
    @Operation(summary = "根据id查询接口")
    @RequiresPermissions("device:cabinet:query")
    @GetMapping("{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        Cabinet serviceById = cabinetService.getById(id);
        AjaxResult ajaxResult = success(serviceById);
        return ajaxResult;
    }


    /**
     * 柜机分页查询接口
     *
     * @param cabinet
     * @return
     */
    @Operation(summary = "柜机分页查询接口")
    @RequiresPermissions("device:cabinet:list")
    @GetMapping("/list")
    public TableDataInfo list(Cabinet cabinet) {
        //设置分页参数
        startPage();
        //调用service方法
        List<Cabinet> cabinetList = cabinetService.selectListCabinet(cabinet);
        TableDataInfo dataTable = getDataTable(cabinetList);
        return dataTable;
    }


    /**
     * 获取充电宝柜机全部详细信息
     *
     * @param id
     * @return
     */
    @Operation(summary = "获取充电宝柜机全部详细信息")
    @RequiresPermissions("device:cabinet:query")
    @GetMapping(value = "/getAllInfo/{id}")
    public AjaxResult getAllInfo(@PathVariable("id") Long id) {
        return success(cabinetService.getAllInfo(id));
    }

}
